package com.meixiang.activity.homes.shopping;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.model.HttpParams;
import com.lzy.okhttputils.request.BaseRequest;
import com.meixiang.R;
import com.meixiang.adapter.shopping.PrivilegeSuitListExpandableAdapter;
import com.meixiang.entity.shopping.GoodsGroupDetailEntity;
import com.meixiang.entity.shopping.result.GoodsGroupDetailResult;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 优惠套装
 *
 * @author LZRUI
 */
public class PrivilegeSuitActivity extends BaseActivity implements PrivilegeSuitListExpandableAdapter.GroupGoodsClickListener {

    private static final String TAG = "PrivilegeSuitActivity";

    public static String GOODS_ID = "goodsId";

    @Bind(R.id.eplv_suit)
    ExpandableListView elvSuit;

    private PrivilegeSuitListExpandableAdapter adapter;
    private GoodsGroupDetailResult goodsGroupDetailResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_privilege_suit);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        HttpUtils.tagCancelRequest(TAG);
    }

    @Override
    protected void initTitle() {
        setTitle("优惠套装");
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void initView() {
        // 去掉组的指示器
        elvSuit.setGroupIndicator(null);
        adapter = new PrivilegeSuitListExpandableAdapter(this);
        elvSuit.setAdapter(adapter);
        adapter.setGroupGoodsClickListener(this);
    }

    @Override
    protected void setData() {
        String goodsId = getIntent().getStringExtra(GOODS_ID);
        HttpParams params = new HttpParams();
        params.put("goodsId", goodsId);
        HttpUtils.post(Config.GOODS_GROUP_DETAIL, TAG, params, new HttpCallBack(PrivilegeSuitActivity.this) {

            @Override
            public void onBefore(BaseRequest request) {
                if (!Tool.isNetworkAvailable(activity)) {
                    OkHttpUtils.getInstance().cancelTag(PrivilegeSuitActivity.this);
                    if (status == null) return;
                    status.showNoNewWork(null);
                } else {
                    if (status == null) return;
                    status.showLoading();
                }
            }

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                goodsGroupDetailResult = AbJsonUtil.fromJson(result.toString(), GoodsGroupDetailResult.class);
                if (goodsGroupDetailResult != null) {
                    List<GoodsGroupDetailEntity> groupList = goodsGroupDetailResult.getGoodsGroupList();
                    if (groupList != null) {
                        adapter.refreshData(groupList);
                    }
                }
            }

            @Override
            public void onError(String code, String message) {

            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                if (status == null) return;
                if (adapter == null || adapter.getGroupCount() == 0) {
                    status.showNoData("没有可用购买的套装，请刷新试试", "立即刷新", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            setData();
                        }
                    });
                } else {
                    status.removeAttachView();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void addToCart(int groupPosition, int childPosition) {
        if (goodsGroupDetailResult == null || goodsGroupDetailResult.getGoodsGroupList() == null)
            return;
        GoodsGroupDetailEntity groupDetailEntity = goodsGroupDetailResult.getGoodsGroupList().get(groupPosition);
        if (groupDetailEntity == null)
            return;
        addToCart(goodsGroupDetailResult.getStoreId(), groupDetailEntity.getGoodsStorePrice(), groupDetailEntity.getGoodsId());
    }

    @Override
    public void showDetail(int groupPosition, int childPosition) {
        if (goodsGroupDetailResult == null || goodsGroupDetailResult.getGoodsGroupList() == null)
            return;
        GoodsGroupDetailEntity groupDetailEntity = goodsGroupDetailResult.getGoodsGroupList().get(groupPosition);
        if (groupDetailEntity == null || groupDetailEntity.getGoodsSpecList() == null)
            return;
        Intent intent = new Intent(this, GoodsDetailActivity.class);
        intent.putExtra(GoodsDetailActivity.GOODS_ID, groupDetailEntity.getGoodsSpecList().get(childPosition).getGoodsId());
        startActivity(intent);
    }

    /**
     * 添加到购物车
     */
    private void addToCart(String storeId, String goodsStorePrice, String goodsId) {
        HttpParams params = new HttpParams();
        params.put("storeId", storeId);
        params.put("spValueIds", "");
        params.put("goodsStorePrice", goodsStorePrice);
        params.put("goodsId", goodsId);
        params.put("goodsNum", "1");

        Log.i("lzrtest", " 套装商品添加到购物车   " + params.toString());

        HttpUtils.post(Config.ADD_TO_CART, TAG, params, new HttpCallBack(PrivilegeSuitActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Toast.makeText(PrivilegeSuitActivity.this, message, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String code, String message) {
                Toast.makeText(PrivilegeSuitActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
